package com.arvato.minipro.controller.admin.mall;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.minipro.bean.bean_do.mall.CommodityImgDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.service.mall.CommodityImgService;


/**
 * Describe：后台管理商品的Controller
 *
 * @author smallking
 *
 *  2018年2月8日
 */
@Controller
@RequestMapping(value = "/admin/mall/img")
public class AdminCommodityImgController {
	
	private Logger logger = LoggerFactory.getLogger(AdminCommodityImgController.class);
	
	@Autowired
	private CommodityImgService commodityImgService;
	
	@Autowired
	private FtpTool ftptool;
	/**
	 * 查询商品图片列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData imgList(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData pageData = commodityImgService.listCommodityImgByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 上传商品图片
	 * @param file
	 * @param commodityId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/upload", method = {RequestMethod.POST})
	public Result uploadImg(@RequestParam("file") MultipartFile file, String commodityId){
		Result result = new Result(false);
		CommodityImgDo commodityImg = new CommodityImgDo();
		commodityImg.setImgName(file.getOriginalFilename());
		commodityImg.setImgSize(Integer.valueOf(file.getSize() + ""));
		// 存储在本地
		/*FileInfo fileInfo = FileUtil.saveFile(file);*/
		// 存储在远程服务器上
		String fileName = ftptool.uploadFile(file);
		if(fileName == null) {
			logger.info("图片上传失败");
			result.setMsg("图片上传失败");
			return result;
		}
		commodityImg.setImgPath(fileName);
		if(commodityId != null){
			commodityImg.setCommodityId(commodityId);
		}
		try {
			int row = commodityImgService.insert(commodityImg);
			if(row == 1){
				result.setResult(true);
				result.setData(commodityImg.getImgId());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return result;
	}
	
	/**
	 * 更新商品图片
	 * @param imgDo
	 * @param type
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result imgUp(CommodityImgDo imgDo, @RequestParam(value="type", defaultValue="")String type){
		Result result = new Result(false);
		try {
			int row = commodityImgService.update(imgDo);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 删除商品图片
	 * @param imgId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String imgId){
		Result result = new Result(false);
		try {
			int row  = commodityImgService.delete(imgId);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
